<?
require_once($r."_config/db.php");
require_once($r."_engine/c_sql_table.php");


$g_st_categories = new CSqlTable("categories", "id");

$g_default_category_name = "General";

class CCategories
{
	var $account_id, $names, $default_category_id;
	
	function CCategories(&$account)
	{
		$this->account_id = $account->id;
		$this->Load();
	}
    
    function MakeNameForCmp($name)
    {
        // remove spaces
        $name = str_replace(" ", "", $name);
        // make upper case
        $name = strtoupper($name);
        
        return $name;
    }
	
	function CreateNew($name, $reload=false)
	{
		$name = trim($name);

		if ($name == "") 
		{
			global $g_default_category_name;
			$name = $g_default_category_name;
		}
		
		// Check if already exists
        $name_for_cmp = $this->MakeNameForCmp($name);
        foreach($this->names as $k => $v) {
            if ($name_for_cmp == $this->MakeNameForCmp($v)) {
                return $k;
            }
        }  
        //Old approach (without case insensetivity and removed spaces)
		//$id = array_search($name, $this->names, false);
		//if ($id !== false) return $id;
		
		global $g_st_categories;
		$g_st_categories->Insert(array(
			"account_id"	=> $this->account_id,
			"name"			=> $name
		));
		$id = mysql_insert_id();
		
		if ($reload) $this->Load();
		return $id;
	}
	
	function Load()
	{
		$this->names = array();

		// Check existance of default category, if not exist - create it		
		global $g_default_category_name;
		$res = execute_query("SELECT * FROM categories ".
			"WHERE account_id=".$this->account_id." ".
			"AND name='".$g_default_category_name."';");
		if (@mysql_num_rows($res) < 1) 
		{
			$this->default_category_id = $this->CreateNew($g_default_category_name);
		}
		else 
		{
			$row = @mysql_fetch_array($res);
			$this->default_category_id = $row["id"];
		}
	
		// Get monitors without categories
		$q = "SELECT DISTINCT monitors.category_id AS m_c_id FROM monitors ".
			"LEFT JOIN categories ON (monitors.category_id = categories.id) ".
			"WHERE monitors.account_id = '".$this->account_id."' ".
			"AND categories.id IS NULL;";
		$res = execute_query($q);
		$ids = array();
		while ($row = @mysql_fetch_array($res))
		{
			$ids[] = $row["m_c_id"];
		}
		//var_dump($ids);
		
		if (count($ids) > 0)
		{
			$w="";
			foreach ($ids as $id)
			{
				if ($w != "") $w .= " OR ";
				$w .= "category_id = $id";
			}
			//var_dump($w);
			
	
			$q = "UPDATE monitors SET category_id = ".$this->default_category_id." ".
				"WHERE monitors.account_id = '".$this->account_id."' AND ".
				"($w);";
			$res = execute_query($q);
		}

		
		$res = execute_query("SELECT * FROM categories WHERE account_id=".$this->account_id." ORDER BY name ASC;");
		while ($row = @mysql_fetch_array($res))
		{
			global $g_st_categories;
			$g_st_categories->DecodeValues($row);

			$this->names[$row["id"]] = $row["name"];
		}
	}
	
	function Delete($ids)
	{
		if (!is_array($ids)) return;
		
		$ids2 = array();
		foreach ($ids as $id) {
			if ($id != $this->default_category_id) $ids2[] = $id;
		}
		
		global $g_st_categories;	
		$g_st_categories->Delete($ids2);
		
		$this->Load();
	}
}
?>